package com.test.spring;

import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    /**
     * Spring允许在Bean创建之前，读取Bean的元属性，并根据自己的需求对元属性进行改变，比如将Bean的scope从singleton改变为prototype，
     * 最典型的应用应当是PropertyPlaceholderConfigurer，替换xml文件中的占位符，
     * 替换为properties文件中相应的key对应的value，这将会在下篇文章中专门讲解PropertyPlaceholderConfigurer的作用及其原理。
     * @param configurableListableBeanFactory
     * @throws BeansException
     */
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        System.out.println(" Enter FactoryPostProcessorBean.postProcessBeanFactory()  \n");
        BeanDefinition beanDefinition = configurableListableBeanFactory.getBeanDefinition("cycleBean");
        MutablePropertyValues beanProperty = beanDefinition.getPropertyValues();
        beanProperty.add("definition","new add definition");
        System.out.println("scope before change：" + beanDefinition.getScope());
//        beanDefinition.setScope("singleton");
//        System.out.println("scope after change：" + beanDefinition.getScope());
        System.out.println("beanProperty：" + beanProperty + "\n");
        //直接可以获取beandefinition，简直无敌
    }
}
